package com.dji.compose_learning

import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.dji.compose_learning.ui.theme.Compose_learningTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val pageList =
            packageManager.getPackageInfo(packageName,
                PackageManager.GET_ACTIVITIES).activities.filter {
                it.name.contains("com.dji.compose_learning") &&
                        !it.name.contains("MainActivity")
            }

        setContent {
            MaterialTheme {
                // A surface container using the 'background' color from the theme
                val context = LocalContext.current
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    LazyColumn(modifier = Modifier.fillMaxSize()) {
                        items(pageList.size) {
                            Text(text = stringResource(id = pageList[it].labelRes),
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(10.dp)
                                    .clickable {
                                        context.startActivity(Intent().apply {
                                            component = ComponentName(context, pageList[it].name)
                                        })
                                    })
                        }
                    }
                }
            }
        }
    }
}